package com.hiabr.iep.common.utils.validate.impl;


import com.hiabr.iep.common.utils.validate.NotContainsCharacter;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * 不包含特定字符验证器
 */
public class NotContainsCharacterValidator implements ConstraintValidator<NotContainsCharacter, String> {

    private List<String> characterList;

    @Override
    public void initialize(NotContainsCharacter constraintAnnotation) {
        characterList = Arrays.asList(constraintAnnotation.value());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        for (String s : characterList) {
            if (value.contains(s)) {
                return false;
            }
        }
        return true;
    }
}
